import { createStore } from "vuex";
import { $getTenantId, $getPark } from "@/utils/api.js";

// import Vuex from "vuex";
// new Vuex.Store();
export default createStore({
  state: {
    facilities: "",//园区下的温室分类
    park_id: "",//园区
    tenant_id: "",
    tenantOpts: [],
    parkOpts: [],
    facilitieName: "",//园区下的温室名称
  },
  mutations: {
    setFaci(state, data) {
      state.facilities = data;
    },
    setParkId(state, data) {
      state.park_id = data;
    },
    setTenantId(state, data) {
      state.tenant_id = data;
    },
    setTenantOpts(state, data) {
      state.tenantOpts = data;
    },
    setParkOpts(state, data) {
      state.parkOpts = data;
    },
    setfacilitieName(state, data) {
      state.facilitieName = data
    }
  },
  actions: {
    async getTenantId({ commit }) {
      const res = await $getTenantId();
      const data = res.results.map((item) => {
        return {
          value: item?.tenant_id,
          label: item?.tenant_name,
        };
      });
      commit("setTenantOpts", data);
      commit("setTenantId", data[0].value);
    },
    async getPark({ commit, state }) {
      const res = await $getPark({ tenant_id: state.tenant_id });
      console.log(res)
      const data = res.results.map((item) => {
        return {
          label: item?.name,
          value: item?.id,
          children: item?.children,
        };
      });
      commit("setParkOpts", data);
    },
  },
});
